<?php

namespace app\admin\model;

use think\Model;

class HeyOrderRecord extends Model
{
    // 表名
    protected $name = 'hey_order_record';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'create_time_text',
        'record_time_text',
        'record_status_text'
    ];
    

    
    public function getRecordStatusList()
    {
        return ['undeclared' => __('Record_status undeclared'),'submitted' => __('Record_status submitted'),'processing' => __('Record_status processing'),'success' => __('Record_status success'),'failure' => __('Record_status failure'),'except' => __('Record_status except')];
    }     


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getRecordTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['record_time']) ? $data['record_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getRecordStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['record_status']) ? $data['record_status'] : '');
        $list = $this->getRecordStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setRecordTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


    public function admin()
    {
        return $this->belongsTo('Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function order()
    {
        return $this->belongsTo('Order', 'order_sn', 'order_sn', [], 'LEFT')->setEagerlyType(0);
    }
}
